package com.zhong.gang.app.ui.zhuchang.input;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import com.zhong.gang.app.utils.ShouFaView;
import com.zhong.gang.applib.ui.ZhangKouView;
import com.zhong.gang.applib.utils.NumberUtils;

import java.util.Locale;

public class ZhuChangMonenyController implements TextWatcher , ShouFaView.MonenyChangedListener,ZhangKouView.ZhangKouListener{
    private EditText mWeightEdt,pWeightEdT,kouWeightEdt;
    private TextView jWeightTv,jsWightTv,money;
    private ShouFaView shouFaView;
    private ZhangKouView zhangKouView;

    public ZhuChangMonenyController(EditText mWeightEdt, EditText pWeightEdT, EditText kouWeightEdt
            , TextView jWeightTv,TextView jsWightTv, TextView money, ShouFaView shouFaView, ZhangKouView zhangKouView) {
        this.mWeightEdt = mWeightEdt;
        this.pWeightEdT = pWeightEdT;
        this.kouWeightEdt = kouWeightEdt;
        this.jWeightTv =jWeightTv;
        this.jsWightTv =jsWightTv;
        this.zhangKouView = zhangKouView;
        this.money =money;
        this.shouFaView = shouFaView;
        zhangKouView.setListener(this);
        this.zhangKouView.getBinding().monenyEdt.addTextChangedListener(this);
        shouFaView.setListener(this);
        mWeightEdt.addTextChangedListener(this);
        pWeightEdT.addTextChangedListener(this);
        kouWeightEdt.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        double mWeight = getWeight(mWeightEdt);
        double pWeight = getWeight(pWeightEdT);
        double jWeight = mWeight - pWeight ;
        jWeight = jWeight >0 ?jWeight :0;
        jWeightTv.setText(String.format(Locale.CHINA,"%.2f",(float)jWeight));
        double kWeight = getWeight(kouWeightEdt);
        double jsWeight = jWeight - kWeight;
        jsWeight = jsWeight >0 ? jsWeight :0;
        jsWightTv.setText(String.format(Locale.CHINA,"%.2f",(float)jsWeight));
        shouFaView.setAllWeight(jsWeight);
    }

    public double getWeight(TextView textView){
        return NumberUtils.getNumber(textView.getText().toString());
    }

    @Override
    public void afterMonenyChanged(double allMoneny) {
        zhangKouView.setMoneny(allMoneny);

    }

    @Override
    public void onZhangKou(double money) {
        this.money.setText(money+"");
    }
}
